package com.hfkh.keyun.controller.backend;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hfkh.keyun.common.ApiResult;
import com.hfkh.keyun.entity.Course;
import com.hfkh.keyun.entity.Major;
import com.hfkh.keyun.entity.Unit;
import com.hfkh.keyun.exception.BusinessException;
import com.hfkh.keyun.mapper.MajorMapper;
import com.hfkh.keyun.mapper.UnitMapper;
import com.hfkh.keyun.service.CourseService;
import com.hfkh.keyun.vo.CourseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
@RestController
@RequestMapping("/api/course")
@Api(tags = "课程管理")
public class CourseController {
    @Resource
    private CourseService courseService;
    @Resource
    private UnitMapper mapper;
    @Resource
    private MajorMapper majorMapper;
    /**
     * 查询所有课程消息
     */
    @ApiOperation("查询课程信息")
    @GetMapping("/queryAllCourse")
    public List<Course> getCourse(){
        List<Course> courses = courseService.list(null);
        courses.forEach(System.out::println);
        return courses;
    }
    /**
     * 查询课程(按照课程id名字)
     */
    @ApiOperation("按照课程id名称查询课程")
    @GetMapping("/queryOneCourse")
    public void getCourseId(@RequestParam("id") Integer id){
        QueryWrapper<Course> wrapper= new QueryWrapper<>();
        wrapper.eq("id",id);
        Course course = courseService.getOne(wrapper);
        Unit unit = mapper.selectById(course.getUnitId());
        course.setUnit(unit);
        Major major = majorMapper.selectById(course.getMajorId());
        course.setMajor(major);
                System.out.println("单元表对象"+unit);
        System.out.println("专业表对象"+major);
        System.out.println("课程名称："+course.getCourseName()+
                "课程详情："+course.getCourseDesc()+
                "课程价格："+course.getCoursePrice()+
                "课程所属单元名称："+course.getUnit().getUnitName()+
                "课程所属专业名称："+course.getMajor().getMajorName());
        //courseService.getCourseId(id);
    }
    /**
     * 根据课程id删除课程信息
     */
    @ApiOperation("根据课程id删除课程信息")
    @DeleteMapping("/deleteCourse")
    public void deleteCourseById(Integer id){
        courseService.removeById(id);
    }
    /**
     * 更改课程
     */
    @ApiOperation("修改课程相关信息")
    @PostMapping("/updateCourseInfo")
    public ApiResult updateCourse(Integer id, CourseVO courseVO) {
        Course course = new Course();
        BeanUtils.copyProperties(courseVO,course);
        course.setId(id);
        courseService.updateById(course);
        return ApiResult.success("修改课程相关信息");
    }
    /**
     * 分页查询：按照课程id
     * @param id
     * @param page
     * @param size
     * @return
     */
    @ApiOperation("按课程id分页查询课程")
    @GetMapping("/queryPage")
    @ApiImplicitParams(
            value = {
                    @ApiImplicitParam(name = "id",value = "编号",required = false,paramType = "query",dataType = "Integer",defaultValue = ""),
                    @ApiImplicitParam(name = "page",value = "页码",required = true,paramType = "query",dataType = "Integer",defaultValue = "1"),
                    @ApiImplicitParam(name = "size",value = "页容量",required = true,paramType = "query",dataType = "Integer",defaultValue = "10")
            }
    )
    public ApiResult queryPage(
            @RequestParam(required = false,defaultValue = "") Integer id, @RequestParam(defaultValue = "1")Integer page,
            @RequestParam(defaultValue = "10") Integer size){
        return courseService.queryPage(id,page,size);
    }
    /**
     * 新增课程
     */
    @ApiOperation("新增课程")
    @PostMapping("/addCourse")
    public ApiResult addCourse(CourseVO courseVO) throws BusinessException, IOException {
//        Course course = new Course();
//        BeanUtils.copyProperties(course,courseVO);
        return courseService.addCourse(courseVO);
    }
}
